package com.fbinggun.dt.task.conditions;

import com.fbinggun.dt.DailyTask;
import com.fbinggun.dt.player.PlayerData;
import com.fbinggun.dt.task.Task;
import com.fbinggun.dt.task.WorldLimit;
import com.fbinggun.fbinggunutil.PApiUtil;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;

import java.lang.reflect.Constructor;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class Conditions implements Listener {
    ConfigurationSection config;
    WorldLimit worldLimit;
    int count;
    String id;
    Task task;
    String msg;
    public Conditions(String id, ConfigurationSection config, Task task) {
        this.id=id;
        this.config = config;
        worldLimit=new WorldLimit(config.getConfigurationSection("WorldLimit"));
        count=config.getInt("Count");
        msg= config.getString("Msg");
        DailyTask.plugin.getServer().getPluginManager().registerEvents(this,DailyTask.plugin);
        this.task=task;
    }

    public String getMsg(Player player) throws SQLException, ClassNotFoundException {
        String m = PApiUtil.setPlaceholders(player,msg);
        PlayerData playerData = DailyTask.getPlayerData(player);
        m=m.replaceAll("\\{Count}", String.valueOf(playerData.getCount(this)));
        m=m.replaceAll("\\{Max}", String.valueOf(getCount()));
        return m;
    }

    public Task getTask() {
        return task;
    }

    public ConfigurationSection getConfig() {
        return config;
    }

    public WorldLimit getWorldLimit() {
        return worldLimit;
    }

    public int getCount() {
        return count;
    }

    public static Map<String, Class<?>> getClassStringMap() {
        return classStringMap;
    }

    public String getId() {
        return id;
    }

    public  static Map<String,Class<?>> classStringMap= new HashMap<>();
    public static void loadTriggerType(){
        classStringMap.put("KillEntity", KillEntity.class);
        classStringMap.put("Move", Move.class);
        classStringMap.put("BlockBreak", BlockBreak.class);
        classStringMap.put("Bred", Bred.class);
        classStringMap.put("CraftItem", CraftItem.class);
        classStringMap.put("EnchantItem", EnchantItem.class);
        classStringMap.put("Fish", Fish.class);
        classStringMap.put("FurnaceExtract", FurnaceExtract.class);
        classStringMap.put("GlidingMove", GlidingMove.class);
        classStringMap.put("InteractBlock", InteractBlock.class);
        classStringMap.put("InteractEntity", InteractEntity.class);
        classStringMap.put("KillPlayer", KillPlayer.class);
        classStringMap.put("ShearEntity", ShearEntity.class);
        classStringMap.put("SneakingMove", SneakingMove.class);
        classStringMap.put("SprintingMove", SprintingMove.class);
        classStringMap.put("VehicleMove", VehicleMove.class);
        classStringMap.put("Box", Box.class);
        classStringMap.put("KillMArena", KillMArena.class);
        classStringMap.put("NewQh", NewQh.class);
    }



    public static <T extends Conditions> Map<String,Conditions> newConditions(ConfigurationSection config,Task t){
        Map<String,Conditions> conditionsList= new HashMap<>();
        if(config!=null){
            for(String id:config.getKeys(false)){
                ConfigurationSection section = config.getConfigurationSection(id);
                String type =section.getString("ConditionsType");
                if(classStringMap.get(type)!=null){
                    Class c = classStringMap.get(type);
                        try {
                            Constructor<?> constructor = c.getConstructor(String.class,ConfigurationSection.class,Task.class);
                            Conditions conditions = (Conditions) constructor.newInstance(id,section,t);
                            conditionsList.put(id,conditions);
                            System.out.println("条件注册"+id+" Type "+type);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                }
            }
        }
        return conditionsList;
    }
    public static void unload(){
        classStringMap=new HashMap<>();
    }

    public boolean isRun(Player player) throws SQLException, ClassNotFoundException {
        PlayerData playerData =DailyTask.getPlayerData(player);
        if(playerData.getTask()==null){
            return false;
        }
        if(task!=null&&task==this.task) {
            if (getWorldLimit().isWorld(player.getWorld())) {
                return false;
            }
        }else {
            return  false;
        }
        return true;
    }

    public void addCount(Player player, double count) throws SQLException, ClassNotFoundException {
        PlayerData playerData = DailyTask.getPlayerData(player);
        playerData.addCount(this,count,player);
        if(msg!=null) {
            player.spigot().sendMessage(ChatMessageType.ACTION_BAR,new TextComponent(getMsg(player)));
        }
    }
}
